home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2004 New Year / INTERNET112.ISO / pc / software / windows / building / easy_php / easyphp16_setup.exe / {app} / phpmyadmin / sql.php < prev    next >
Encoding:
PHP Script  |  2003-09-24  |  17.3 KB  |  477 lines

  1. <?php
  2. /* $Id: sql.php,v 1.94 2002/04/21 11:48:34 loic1 Exp $ */
  3.  
  4.  
  5. /**
  6.  * Gets some core libraries
  7.  */
  8. require('./libraries/grab_globals.lib.php');
  9. require('./libraries/common.lib.php');
  10.  
  11.  
  12. /**
  13.  * Defines the url to return to in case of error in a sql statement
  14.  */
  15. if (empty($goto)) {
  16.     $goto    = (empty($table)) ? 'db_details.php' : 'tbl_properties.php';
  17. }
  18. if (!isset($err_url)) {
  19.     $err_url = $goto
  20.              . '?lang=' . $lang
  21.              . '&server=' . $server
  22.              . (isset($db) ? '&db=' . urlencode($db) : '')
  23.              . (($goto != 'db_details.php' && isset($table)) ? '&table=' . urlencode($table) : '');
  24. }
  25.  
  26.  
  27. /**
  28.  * Check rights in case of DROP DATABASE
  29.  *
  30.  * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
  31.  * but since a malicious user may pass this variable by url/form, we don't take
  32.  * into account this case.
  33.  */
  34. if (!defined('PMA_CHK_DROP')
  35.     && !$cfgAllowUserDropDatabase
  36.     && eregi('DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE[[:space:]]', $sql_query)) {
  37.     // Checks if the user is a Superuser
  38.     // TODO: set a global variable with this information
  39.     // loic1: optimized query
  40.     $result = @mysql_query('USE mysql');
  41.     if (mysql_error()) {
  42.         include('./header.inc.php');
  43.         PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
  44.     } // end if
  45. } // end if
  46.  
  47.  
  48. /**
  49.  * Bookmark add
  50.  */
  51. if (isset($store_bkm)) {
  52.     if (get_magic_quotes_gpc()) {
  53.         $fields['label'] = stripslashes($fields['label']);
  54.     }
  55.     include('./libraries/bookmark.lib.php');
  56.     PMA_addBookmarks($fields, $cfgBookmark);
  57.     header('Location: ' . $cfgPmaAbsoluteUri . $goto);
  58. }
  59.  
  60.  
  61. /**
  62.  * Gets the true sql query
  63.  */
  64. // $sql_query has been urlencoded in the confirmation form for drop/delete
  65. // queries or in the navigation bar for browsing among records
  66. if (isset($btnDrop) || isset($navig)) {
  67.     $sql_query = urldecode($sql_query);
  68. }
  69.  
  70. // If the query is a Select, extract the db and table names and modify
  71. // $db and $table, to have correct page headers, links and left frame.
  72. // db and table name may be enclosed with backquotes, db is optionnal,
  73. // query may contain aliases.
  74. // (todo: check for embedded comments...)
  75.  
  76. $is_select = eregi('^SELECT[[:space:]]+', $sql_query);
  77. if ($is_select) {
  78.     eregi('^SELECT[[:space:]]+(.*)[[:space:]]+FROM[[:space:]]+(`[^`]+`|[A-Za-z0-9_$]+)([\.]*)(`[^`]*`|[A-Za-z0-9_$]*)', $sql_query, $tmp);
  79.  
  80.     if ($tmp[3] == '.') {
  81.         $prev_db = $db;
  82.         $db      = str_replace('`', '', $tmp[2]);
  83.         $reload  = ($db == $prev_db) ? 0 : 1;
  84.         $table   = str_replace('`', '', $tmp[4]);
  85.     }
  86.     else {
  87.         $table   = str_replace('`', '', $tmp[2]);
  88.     }
  89. } // end if
  90.  
  91.  
  92. /**
  93.  * Sets or modifies the $goto variable if required
  94.  */
  95. if ($goto == 'sql.php') {
  96.     $goto = 'sql.php'
  97.           . '?lang=' . $lang
  98.           . '&server=' . $server
  99.           . '&db=' . urlencode($db)
  100.           . '&table=' . urlencode($table)
  101.           . '&pos=' . $pos
  102.           . '&sql_query=' . urlencode($sql_query);
  103. }
  104.  
  105.  
  106. /**
  107.  * Go back to further page if table should not be dropped
  108.  */
  109. if (isset($btnDrop) && $btnDrop == $strNo) {
  110.     if (!empty($back)) {
  111.         $goto = $back;
  112.     }
  113.     if (@file_exists('./' . $goto)) {
  114.         if ($goto == 'db_details.php' && !empty($table)) {
  115.             unset($table);
  116.         }
  117.         include('./' . ereg_replace('\.\.*', '.', $goto));
  118.     } else {
  119.         header('Location: ' . $cfgPmaAbsoluteUri . str_replace('&', '&', $goto));
  120.     }
  121.     exit();
  122. } // end if
  123.  
  124.  
  125. /**
  126.  * Displays the confirm page if required
  127.  *
  128.  * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
  129.  * with js) because possible security issue is not so important here: at most,
  130.  * the confirm message isn't displayed.
  131.  */
  132. if (!$cfgConfirm
  133.     || (isset($is_js_confirmed) && $is_js_confirmed)
  134.     || isset($btnDrop)) {
  135.     $do_confirm = FALSE;
  136. } else {
  137.     $do_confirm = (eregi('DROP[[:space:]]+(IF[[:space:]]+EXISTS[[:space:]]+)?(TABLE|DATABASE[[:space:]])|ALTER[[:space:]]+TABLE[[:space:]]+((`[^`]+`)|([A-Za-z0-9_$]+))[[:space:]]+DROP[[:space:]]|DELETE[[:space:]]+FROM[[:space:]]', $sql_query));
  138. }
  139.  
  140. if ($do_confirm) {
  141.     if (get_magic_quotes_gpc()) {
  142.         $stripped_sql_query = stripslashes($sql_query);
  143.     } else {
  144.         $stripped_sql_query = $sql_query;
  145.     }
  146.     include('./header.inc.php');
  147.     echo $strDoYouReally . ' :<br />' . "\n";
  148.     echo '<tt>' . htmlspecialchars($stripped_sql_query) . '</tt> ?<br/>' . "\n";
  149.     ?>
  150. <form action="sql.php" method="post">
  151.     <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
  152.     <input type="hidden" name="server" value="<?php echo $server; ?>" />
  153.     <input type="hidden" name="db" value="<?php echo $db; ?>" />
  154.     <input type="hidden" name="table" value="<?php echo isset($table) ? $table : ''; ?>" />
  155.     <input type="hidden" name="sql_query" value="<?php echo urlencode($sql_query); ?>" />
  156.     <input type="hidden" name="zero_rows" value="<?php echo isset($zero_rows) ? $zero_rows : ''; ?>" />
  157.     <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  158.     <input type="hidden" name="back" value="<?php echo isset($back) ? $back : ''; ?>" />
  159.     <input type="hidden" name="reload" value="<?php echo isset($reload) ? $reload : 0; ?>" />
  160.     <input type="hidden" name="show_query" value="<?php echo isset($show_query) ? $show_query : ''; ?>" />
  161.     <input type="submit" name="btnDrop" value="<?php echo $strYes; ?>" />
  162.     <input type="submit" name="btnDrop" value="<?php echo $strNo; ?>" />
  163. </form>
  164.     <?php
  165.     echo "\n";
  166. } // end if
  167.  
  168.  
  169. /**
  170.  * Executes the query and displays results
  171.  */
  172. else {
  173.     if (!isset($sql_query)) {
  174.         $sql_query = '';
  175.     } else if (get_magic_quotes_gpc()) {
  176.         $sql_query = stripslashes($sql_query);
  177.     }
  178.     // Defines some variables
  179.     // loic1: A table have to be created -> left frame should be reloaded
  180.     if ((!isset($reload) || $reload == 0)
  181.         && eregi('^CREATE TABLE[[:space:]]+(.*)', $sql_query)) {
  182.         $reload           = 1;
  183.     }
  184.     // Gets the number of rows per page
  185.     if (!isset($session_max_rows)) {
  186.         $session_max_rows = $cfgMaxRows;
  187.     } else if ($session_max_rows != 'all') {
  188.         $cfgMaxRows       = $session_max_rows;
  189.     }
  190.     // Defines the display mode (horizontal/vertical) and header "frequency"
  191.     if (empty($disp_direction)) {
  192.         $disp_direction   = $cfgDefaultDisplay;
  193.     }
  194.     if (empty($repeat_cells)) {
  195.         $repeat_cells     = $cfgRepeatCells;
  196.     }
  197.  
  198.     $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = FALSE;
  199.     if ($is_select) { // see line 76
  200.         $is_count    = (eregi('^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)', $sql_query));
  201.         $is_export   = (eregi('[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+', $sql_query));
  202.     } else if (eregi('^EXPLAIN[[:space:]]+', $sql_query)) {
  203.         $is_explain  = TRUE;
  204.     } else if (eregi('^DELETE[[:space:]]+', $sql_query)) {
  205.         $is_delete   = TRUE;
  206.         $is_affected = TRUE;
  207.     } else if (eregi('^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+', $sql_query)) {
  208.         $is_insert   = TRUE;
  209.         $is_affected = TRUE;
  210.     } else if (eregi('^UPDATE[[:space:]]+', $sql_query)) {
  211.         $is_affected = TRUE;
  212.     } else if (eregi('^SHOW[[:space:]]+', $sql_query)) {
  213.         $is_show     = TRUE;
  214.     } else if (eregi('^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+', $sql_query)) {
  215.         $is_maint    = TRUE;
  216.     }
  217.  
  218.     // Do append a "LIMIT" clause?
  219.     if (isset($pos)
  220.         && (!$cfgShowAll || $session_max_rows != 'all')
  221.         && $is_select
  222.         && !($is_count || $is_export)
  223.         && eregi('[[:space:]]FROM[[:space:]]', $sql_query)
  224.         && !eregi('[[:space:]]LIMIT[[:space:]0-9,]+$', $sql_query)) {
  225.  
  226.         $sql_limit_to_append = " LIMIT $pos, $cfgMaxRows";
  227.         if (eregi('(.*)([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))$', $sql_query, $regs)) {
  228.             $full_sql_query  = $regs[1] . $sql_limit_to_append . $regs[2];
  229.         } else {
  230.             $full_sql_query  = $sql_query . $sql_limit_to_append;
  231.         }
  232.     } else {
  233.         $full_sql_query      = $sql_query;
  234.     } // end if...else
  235.  
  236.  
  237.     mysql_select_db($db);
  238.  
  239.     // If the query is a DELETE query with no WHERE clause, get the number of
  240.     // rows that will be deleted (mysql_affected_rows will always return 0 in
  241.     // this case)
  242.     if ($is_delete
  243.         && eregi('^DELETE([[:space:]].+)?([[:space:]]FROM[[:space:]](.+))$', $sql_query, $parts)
  244.         && !eregi('[[:space:]]WHERE[[:space:]]', $parts[3])) {
  245.         $OPresult     = @mysql_query('SELECT COUNT(*) as count' .  $parts[2]);
  246.         if ($OPresult) {
  247.             $num_rows = mysql_result($OPresult, 0, 'count');
  248.         } else {
  249.             $num_rows = 0;
  250.         }
  251.         mysql_free_result($OPresult);
  252.     }
  253.  
  254.     // Executes the query
  255.     $result   = @mysql_query($full_sql_query);
  256.  
  257.     // Displays an error message if required and stop parsing the script
  258.     if (mysql_error()) {
  259.         $error        = mysql_error();
  260.         include('./header.inc.php');
  261.         $full_err_url = (ereg('^(db_details|tbl_properties)', $err_url))
  262.                       ? $err_url . '&show_query=y&sql_query=' . urlencode($sql_query)
  263.                       : $err_url;
  264.         PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
  265.     }
  266.  
  267.     // tmpfile remove after convert encoding appended by Y.Kawada
  268.     if (function_exists('PMA_kanji_file_conv')
  269.         && (isset($textfile) && file_exists($textfile))) {
  270.         unlink($textfile);
  271.     }
  272.  
  273.     // Gets the number of rows affected/returned
  274.     if (!$is_affected) {
  275.         $num_rows = @mysql_num_rows($result);
  276.     } else if (!isset($num_rows)) {
  277.         $num_rows = @mysql_affected_rows();
  278.     }
  279.  
  280.     // Counts the total number of rows for the same 'SELECT' query without the
  281.     // 'LIMIT' clause that may have been programatically added
  282.     if (empty($sql_limit_to_append)) {
  283.         $unlim_num_rows         = $num_rows;
  284.     }
  285.     else if ($is_select) {
  286.         // reads only the from-part of the query...
  287.         $sp='[[:space:]]';
  288.         $array = split(
  289.             $sp . 'from' . $sp .'|' . $sp . 'FROM' .$sp .
  290.             '|' . $sp .'order' . $sp . '|' . $sp . 'ORDER' . $sp .
  291.             '|' . $sp .'having' . $sp . '|' . $sp . 'HAVING' . $sp .
  292.             '|' . $sp .'limit' . $sp . '|' . $sp . 'LIMIT' . $sp .
  293.             '|' . $sp .'group' . $sp . 'by'. $sp .
  294.             '|' . $sp . 'GROUP' . $sp . 'BY' . $sp, $sql_query);
  295.         if (!empty($array[1])) {
  296.             // ... and makes a count(*) to count the entries
  297.             $count_query = 'SELECT COUNT(*) AS count FROM ' . $array[1];
  298.             $OPresult    = mysql_query($count_query);
  299.             if ($OPresult) {
  300.                 $unlim_num_rows = mysql_result($OPresult, 0, 'count');
  301.             }
  302.             mysql_free_result($OPresult);
  303.         } else {
  304.             $unlim_num_rows     = 0;
  305.         }
  306.     } // end rows total count
  307.  
  308.     // No rows returned -> move back to the calling page
  309.     if ($num_rows < 1 || $is_affected) {
  310.         if ($is_delete) {
  311.             $message = $strDeletedRows . ' ' . $num_rows;
  312.         } else if ($is_insert) {
  313.             $message = $strInsertedRows . ' ' . $num_rows;
  314.         } else if ($is_affected) {
  315.             $message = $strAffectedRows . ' ' . $num_rows;
  316.         } else if (!empty($zero_rows)) {
  317.             $message = $zero_rows;
  318.         } else {
  319.             $message = $strEmptyResultSet;
  320.         }
  321.  
  322.         if (@file_exists('./' . $goto)) {
  323.             $goto = ereg_replace('\.\.*', '.', $goto);
  324.             // Checks for a valid target script
  325.             if (isset($table) && $table == '') {
  326.                 unset($table);
  327.             }
  328.             if (isset($db) && $db == '') {
  329.                 unset($db);
  330.             }
  331.             $is_db = $is_table = FALSE;
  332.             if ($goto == 'tbl_properties.php') {
  333.                 if (!isset($table)) {
  334.                     $goto     = 'db_details.php';
  335.                 } else {
  336.                     $is_table = @mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
  337.                     if (!@mysql_numrows($is_table)) {
  338.                         $goto = 'db_details.php';
  339.                         unset($table);
  340.                     }
  341.                 } // end if... else...
  342.             }
  343.             if ($goto == 'db_details.php') {
  344.                 if (isset($table)) {
  345.                     unset($table);
  346.                 }
  347.                 if (!isset($db)) {
  348.                     $goto     = 'main.php';
  349.                 } else {
  350.                     $is_db    = @mysql_select_db($db);
  351.                     if (!$is_db) {
  352.                         $goto = 'main.php';
  353.                         unset($db);
  354.                     }
  355.                 } // end if... else...
  356.             }
  357.             // Loads to target script
  358.             if ($goto == 'db_details.php' || $goto == 'tbl_properties.php') {
  359.                 $js_to_run = 'functions.js';
  360.             }
  361.             if ($goto != 'main.php') {
  362.                 include('./header.inc.php');
  363.             }
  364.             include('./' . $goto);
  365.         } // end if file_exist
  366.         else {
  367.             header('Location: ' . $cfgPmaAbsoluteUri . str_replace('&', '&', $goto) . '&message=' . urlencode($message));
  368.         } // end else
  369.         exit();
  370.     } // end no rows returned
  371.  
  372.     // At least one row is returned -> displays a table with results
  373.     else {
  374.         // Displays the headers
  375.         if (isset($show_query)) {
  376.             unset($show_query);
  377.         }
  378.         $js_to_run = 'functions.js';
  379.         include('./header.inc.php');
  380.         include('./libraries/bookmark.lib.php');
  381.  
  382.         // Gets the list of fields properties
  383.         while ($field = mysql_fetch_field($result)) {
  384.             $fields_meta[] = $field;
  385.         }
  386.         $fields_cnt        = count($fields_meta);
  387.  
  388.         // Displays the results in a table
  389.         include('./libraries/display_tbl.lib.php');
  390.         if (empty($disp_mode)) {
  391.             // see the "PMA_setDisplayMode()" function in
  392.             // libraries/display_tbl.lib.php
  393.             $disp_mode = 'urdr11110';
  394.         }
  395.         PMA_displayTable($result, $disp_mode);
  396.         mysql_free_result($result);
  397.  
  398.         // Displays "Insert a new row" link if required
  399.         if ($disp_mode[6] == '1') {
  400.             $lnk_goto  = 'sql.php'
  401.                        . '?lang=' . $lang
  402.                        . '&server=' . $server
  403.                        . '&db=' . urlencode($db)
  404.                        . '&table=' . urlencode($table)
  405.                        . '&pos=' . $pos
  406.                        . '&session_max_rows=' . $session_max_rows
  407.                        . '&disp_direction=' . $disp_direction
  408.                        . '&repeat_cells=' . $repeat_cells
  409.                        . '&sql_query=' . urlencode($sql_query);
  410.             $url_query = 'lang=' . $lang
  411.                        . '&server=' . $server
  412.                        . '&db=' . urlencode($db)
  413.                        . '&table=' . urlencode($table)
  414.                        . '&pos=' . $pos
  415.                        . '&session_max_rows=' . $session_max_rows
  416.                        . '&disp_direction=' . $disp_direction
  417.                        . '&repeat_cells=' . $repeat_cells
  418.                        . '&sql_query=' . urlencode($sql_query)
  419.                        . '&goto=' . urlencode($lnk_goto);
  420.  
  421.             echo "\n\n";
  422.             echo '<!-- Insert a new row -->' . "\n";
  423.             echo '<p>' . "\n";
  424.             echo '    <a href="tbl_change.php?' . $url_query . '">' . $strInsertNewRow . '</a>' . "\n";
  425.             echo '</p>' . "\n";
  426.         } // end insert new row
  427.  
  428.         // Bookmark Support if required
  429.         if ($disp_mode[7] == '1'
  430.             && ($cfgBookmark['db'] && $cfgBookmark['table'] && empty($id_bookmark))
  431.             && !empty($sql_query)) {
  432.             echo "\n";
  433.  
  434.             $goto = 'sql.php'
  435.                   . '?lang=' . $lang
  436.                   . '&server=' . $server
  437.                   . '&db=' . urlencode($db)
  438.                   . '&table=' . urlencode($table)
  439.                   . '&pos=' . $pos
  440.                   . '&session_max_rows=' . $session_max_rows
  441.                   . '&disp_direction=' . $disp_direction
  442.                   . '&repeat_cells=' . $repeat_cells
  443.                   . '&sql_query=' . urlencode($sql_query)
  444.                   . '&id_bookmark=1';
  445.             ?>
  446. <!-- Bookmark the query -->
  447. <form action="sql.php" method="post" onsubmit="return emptyFormElements(this, 'fields[label]');">
  448.             <?php
  449.             echo "\n";
  450.             if ($disp_mode[3] == '1') {
  451.                 echo '    <i>' . $strOr . '</i>' . "\n";
  452.             }
  453.             ?>
  454.     <br /><br />
  455.     <?php echo $strBookmarkLabel; ?> :
  456.     <input type="hidden" name="server" value="<?php echo $server; ?>" />
  457.     <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  458.     <input type="hidden" name="fields[dbase]" value="<?php echo $db; ?>" />
  459.     <input type="hidden" name="fields[user]" value="<?php echo $cfgBookmark['user']; ?>" />
  460.     <input type="hidden" name="fields[query]" value="<?php echo urlencode($sql_query); ?>" />
  461.     <input type="text" name="fields[label]" value="" />
  462.     <input type="submit" name="store_bkm" value="<?php echo $strBookmarkThis; ?>" />
  463. </form>
  464.             <?php
  465.         } // end bookmark support
  466.     } // end rows returned
  467.  
  468. } // end executes the query
  469. echo "\n\n";
  470.  
  471.  
  472. /**
  473.  * Displays the footer
  474.  */
  475. require('./footer.inc.php');
  476. ?>
  477.